package com.fenqing.leetcode.code.sword_refers_offer_ii;


import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author fenqing
 */
public class Topic083 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic083(String input) {
        super(input);
    }


    @Execute
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        permute(nums, result, new LinkedList<>(), used);
        return result;
    }
    //
    public void permute(int[] nums, List<List<Integer>> result, List<Integer> list, boolean[] used) {
        if (list.size() == nums.length) {
            result.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            list.add(nums[i]);
            permute(nums, result, list, used);
            list.remove(list.size() - 1);
            used[i] = false;
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> run = new Topic083("[1,2,3]").run();
        run.forEach(System.out::println);
    }
}
